plugins {
    id 'com.android.application'
    id("com.google.devtools.ksp")
    id 'kotlin-android'
}

def getVersionCode = { ->
    try {
        def code = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-list', '--count', 'origin/main'
            standardOutput = code
        }
        return Integer.parseInt(code.toString().trim())
    }
    catch (ignored) {
        return 1
    }
}

def getVersionName = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'describe', '--tags', '--dirty'
            standardOutput = stdout
        }
        return stdout.toString().trim().replaceFirst("^v", "")
    }
    catch (ignored) {
        return '0.0.0'
    }
}

android {
    compileSdk 34
    defaultConfig {
        applicationId "top.rootu.lampa"
        minSdkVersion 16
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 28
        versionCode getVersionCode()
        versionName getVersionName()
        // Required when setting minSdkVersion to 20 or lower
        multiDexEnabled = true
        // Use SupportLibrary for vectors
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    signingConfigs {
        release {
            def keystoreProperties = new Properties()
            def keystorePropsFile = file("keystore/keystore_config")

            if (keystorePropsFile.exists()) {
                file("keystore/keystore_config").withInputStream { keystoreProperties.load(it) }
                storeFile file("$keystoreProperties.storeFile")
                storePassword "$keystoreProperties.storePassword"
                keyAlias "$keystoreProperties.keyAlias"
                keyPassword "$keystoreProperties.keyPassword"
            } else {
                storeFile file(System.getenv('KEYSTORE_FILE'))
                storePassword System.getenv('KEYSTORE_PASSWORD')
                keyAlias System.getenv('RELEASE_SIGN_KEY_ALIAS')
                keyPassword System.getenv('RELEASE_SIGN_KEY_PASSWORD')
            }
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        // Flag to enable support for the new language APIs
        // https://developer.android.com/studio/write/java8-support-table
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        resources.excludes += "DebugProbesKt.bin"
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    // gradle 4.6 migration: disable dimensions mechanism
    // more: https://proandroiddev.com/advanced-android-flavors-part-4-a-new-version-fc2ad80c01bb
    flavorDimensions "default"

    productFlavors {
        lite {}
        full {}
    }
    packagingOptions {
        resources.excludes += "DebugProbesKt.bin"
    }
    buildFeatures {
        viewBinding true
    }
    namespace 'top.rootu.lampa'
}

dependencies {
    // https://developer.android.com/studio/write/java8-support#library-desugaring
    // For AGP 7.4+
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // JavaScript to run PAC file to get correct proxies
    implementation 'org.mozilla:rhino:1.7.14'
    // okhttp for android 4.1+ // 3.12.12 - latest version for API 16
    //noinspection GradleDependency
    implementation 'com.squareup.okhttp3:okhttp:3.12.12'
    //noinspection GradleDependency
    implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:3.12.12'
    // multidex
    implementation 'androidx.multidex:multidex:2.0.1'
    // netcipher
    implementation 'info.guardianproject.netcipher:netcipher:2.1.0'
    // androidx
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.lifecycle:lifecycle-process:2.6.2'
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.recommendation:recommendation:1.0.0'
    implementation 'androidx.tvprovider:tvprovider:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'org.brotli:dec:0.1.2'
    //glide
    def glideVersion = '4.16.0'
    implementation "com.github.bumptech.glide:glide:${glideVersion}"
    implementation("com.github.bumptech.glide:okhttp3-integration:${glideVersion}") {
        exclude group: 'glide-parent'
        transitive = false
    }
    ksp "com.github.bumptech.glide:ksp:${glideVersion}"
    implementation "com.github.bumptech.glide:annotations:${glideVersion}"
    // material-ui
    implementation 'com.google.android.material:material:1.10.0'
    // speech
    implementation 'net.gotev:speech:1.6.2'
    // coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
    // json serializer
    //noinspection GradleDependency 2.10.x thorow java.lang.VerifyError on api17
    implementation 'com.google.code.gson:gson:2.9.1'
    // lottie loaders
    implementation 'com.airbnb.android:lottie:6.2.0'
    // tests
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    // webkit
    //noinspection GradleDependency 1.8.0 last api16 compatible
    implementation 'androidx.webkit:webkit:1.8.0'
    // https://download.01.org/crosswalk/releases/crosswalk/android/stable/latest/
    liteApi 'org.xwalk:xwalk_shared_library:23.53.589.4'
    fullApi 'org.xwalk:xwalk_core_library:23.53.589.4'
}